package com.share.im.interactor;

import java.util.Collection;

import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.XMPPError;

import android.os.AsyncTask;

import com.share.im.activity.login.OnLoginFinishedListener;
import com.share.im.common.Constant;
import com.share.im.manager.XmppConnectionManager;
import com.share.im.model.LoginConfig;
import com.share.im.model.User;

/**
 * 登陆、注册 交互器
 * 
 * @author Administrator
 * 
 */
public class LoginInteractor extends InteractorSupport implements ILoginInteractor
{
	// private IUserModel userModel;

	public LoginInteractor()
	{
	}

	@Override
	public void login(final LoginConfig loginConfig, final OnLoginFinishedListener listener)
	{
		new AsyncTask<Void, Void, Integer>()
		{

			@Override
			protected Integer doInBackground(Void... params)
			{
				int resultCode = 0;
				try
				{
					XMPPConnection connection = XmppConnectionManager.getInstance().getConnection();
					connection.connect();
					connection.login(loginConfig.getUsername(), loginConfig.getPassword()); // 登录
					// OfflineMsgManager.getInstance(activitySupport).dealOfflineMsg(connection);//处理离线消息
					connection.sendPacket(new Presence(Presence.Type.available));
					if (loginConfig.isNovisible())
					{// 隐身登录
						Presence presence = new Presence(Presence.Type.unavailable);
						Collection<RosterEntry> rosters = connection.getRoster().getEntries();
						for (RosterEntry rosterEntry : rosters)
						{
							presence.setTo(rosterEntry.getUser());
							connection.sendPacket(presence);
						}
					}
					resultCode = Constant.LOGIN_SECCESS;
					
					
				}
				catch (Exception xee)
				{
					if (xee instanceof XMPPException)
					{
						XMPPException xe = (XMPPException) xee;
						final XMPPError error = xe.getXMPPError();
						int errorCode = 0;
						if (error != null)
						{
							errorCode = error.getCode();
						}
						if (errorCode == 401)
						{
							resultCode = Constant.LOGIN_ERROR_ACCOUNT_PASS;
						}
						else if (errorCode == 403)
						{
							resultCode = Constant.LOGIN_ERROR_ACCOUNT_PASS;
						}
						else
						{
							resultCode = Constant.SERVER_UNAVAILABLE;
						}
					}
					else
					{
						resultCode = Constant.LOGIN_ERROR;
					}
					xee.printStackTrace();
				}
				return resultCode;
			}

			@Override
			protected void onPreExecute()
			{
				// TODO Auto-generated method stub
				super.onPreExecute();
			}

			@Override
			protected void onPostExecute(Integer result)
			{
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				// 登陆返回结果
				listener.onSuccess(result);
			}
		}.execute();
	}

	@Override
	public void regist(User user, OnLoginFinishedListener listener)
	{
		// TODO Auto-generated method stub

	}

}
